<?php
	include("includes/connect_db.php");

	$host = $_POST['host'];
	$sql = $_POST['sql'];
	$sql = strtolower($sql);
	$keyword = $_POST['keyword'];
	$tabel = $_POST['tabel'];
	$output = $_POST['output'];
	$parameter = $_POST['parameter'];
	$fungsi = $_POST['fungsi'];
	$fungsiParameter = $_POST['fungsiParameter'];
	$hakAkses = $_POST['hakAkses'];
	$formatName = $_POST['formatName'];
	$karakter = $_POST['karakter'];
	$format = $keyword;

	// echo $host." | ".$sql." | ".$keyword." | ".$tabel." | ".$output." | ".$parameter." | ".$fungsi." | ".$fungsiParameter." | ".$hakAkses." | ".$formatName;

// ----------------------- CARI ID SQL ----------------------- 
	$sqlIdSql = "SELECT id_jenis_sql FROM tb_jenis_sql WHERE jenis_sql = '$sql'";
	$queryIdSql = mysql_query($sqlIdSql);
	$idSql = mysql_fetch_array($queryIdSql);
	$idSql = $idSql['id_jenis_sql'];

// ----------------------- CARI ID KARAKTER ----------------------- 
	$sqlIdKarakter = "SELECT id_karakter FROM tb_master_karakter WHERE karakter = '$karakter'";
	$queryIdKarakter = mysql_query($sqlIdKarakter);
	$idKarakter = mysql_fetch_array($queryIdKarakter);
	$idKarakter = $idKarakter['id_karakter'];

// ----------------------- PARSING TABEL ----------------------- 
	$i = 1;
    $rincianTabel = explode(',', $tabel);
    $jumlahTabel = count($rincianTabel);
    foreach ($rincianTabel as $parseTabel) {
        $parseTabel = ltrim($parseTabel);
        $arrayTabel[$i] = $parseTabel;
        $i++;
    }

// ----------------------- PARSING OUTPUT ----------------------- 
    $x = 1;
    $y = 1;
    $rincianOutput = explode(',', $output);
    $jumlahOutput = count($rincianOutput);
    if ($jumlahOutput > 1) {
	    foreach ($rincianOutput as $parseOutput) {
	    	$parseOutput = ltrim($parseOutput);
	    	if ($y > 3) {
	    		$y = 1;	
	        	$x++;
	        	// echo "<br/>";
	    	}
	        $arrayOutput[$x][$y] = $parseOutput;
	        // echo $arrayOutput[$x][$y];
	        $y++;
	    }
	    $jumlahOutput = $x;
    } else{
    	$jumlahOutput = 0;
    }
    // echo $jumlahOutput."<br/>";

// ----------------------- PARSING PARAMETER ----------------------- 
	$x = 1;
    $y = 1;
    $rincianParameter = explode(',', $parameter);
    $jumlahParameter = count($rincianParameter);
    if ($jumlahParameter > 1) {
	    foreach ($rincianParameter as $parseParameter) {
	    	if ($y > 3) {
	    		$y = 1;	
	        	$x++;
	        	// echo "<br/>";
	    	}
	    	$parseParameter = ltrim($parseParameter);
	        $arrayParameter[$x][$y] = $parseParameter;
	        // echo $arrayParameter[$x][$y];
	        $y++;
	    }
	    $jumlahParameter = $x;
	} else{
		$jumlahParameter = 0;
	}
	// echo $jumlahParameter."<br/>";

// ----------------------- PARSING FUNGSIPARAMETER ----------------------- 
	$x = 1;
    $y = 1;
    $rincianFungsiParameter = explode(',', $fungsiParameter);
    $jumlahFungsiParameter = count($rincianFungsiParameter);
    if ($jumlahFungsiParameter > 1) {
	    foreach ($rincianFungsiParameter as $parseFungsiParameter) {
	    	if ($y > 2) {
	    		$y = 1;	
	        	$x++;
	        	// echo "<br/>";
	    	}
	    	$parseFungsiParameter = ltrim($parseFungsiParameter);
	        $arrayFungsiParameter[$x][$y] = $parseFungsiParameter;
	        // echo $arrayFungsiParameter[$x][$y];
	        $y++;
	    }
	    $jumlahFungsiParameter = $x;
	} else{
		$jumlahFungsiParameter = 0;
	}
	// echo $jumlahFungsiParameter."<br/>";

	

	// SIMPAN OPERASI =====================================================================================
	$sqlLihatOperasi = "SELECT * FROM tb_operasi WHERE id_host = '$host' and id_jenis_sql = '$idSql' and
						keyword = '$keyword'";
	$queryLihatOperasi = mysql_query($sqlLihatOperasi);
	$jumlah = mysql_num_rows($queryLihatOperasi);

	if ($jumlah > 0) {
		$queryLihatOperasi = mysql_query($sqlLihatOperasi);
		$lihatOperasi = mysql_fetch_array($queryLihatOperasi);
		$id_operasi = $lihatOperasi['id_operasi'];

		$sqlInsertOperasi = "UPDATE tb_operasi SET id_host='$host', id_jenis_sql='$idSql', keyword='$keyword', 
							is_publik='$hakAkses', nama_operasi='$formatName', id_karakter='$idKarakter'
							WHERE id_operasi='$id_operasi'";
	} else{
		// SIMPAN KE TABEL OPERASI
		$sqlInsertOperasi = "INSERT INTO tb_operasi (id_host, id_jenis_sql, id_karakter, keyword, is_publik, nama_operasi)
							VALUES ($host, $idSql, $idKarakter, '$keyword', $hakAkses, '$formatName')";
	}
	$queryInsertOperasi = mysql_query($sqlInsertOperasi);

	// ----------------------------------------------------------------------------------------------------------
	switch ($sql) {
		case 'select':
			// PENGAMBILAN ID PADA TABEL OPERASI
			$sqlID = "SELECT * FROM tb_operasi WHERE keyword = '$keyword' and nama_operasi = '$formatName'";
			$queryID = mysql_query($sqlID);
			$ID = mysql_fetch_array($queryID);
			$ID = $ID['id_operasi'];

			// SIMPAN KE TABEL TABEL
			$i = 1;
			$sqlLihatTabel = "SELECT * FROM tb_operasi_tabel WHERE id_operasi = '$ID' ORDER BY id_operasi_tabel ASC";
			$queryLihatTabel = mysql_query($sqlLihatTabel);
			$jumlah = mysql_num_rows($queryLihatTabel);
			$queryLihatTabel = mysql_query($sqlLihatTabel);
			while ($lihatTabel = mysql_fetch_array($queryLihatTabel)) {
				$id_operasi_tabel[$i] = $lihatTabel['id_operasi_tabel'];
				$i++;
			}

			for ($i=1; $i <= $jumlahTabel; $i++) { 
				$isianNamaTabel = $arrayTabel[$i];
				$id_operasi_tabel = $id_operasi_tabel[$i];
				if ($jumlah > 0) {
					$sqlInsertTabel = "UPDATE tb_operasi_tabel SET nama_tabel = '$isianNamaTabel'
										WHERE id_operasi_tabel = '$id_operasi_tabel'";
				} else{
					$sqlInsertTabel = "INSERT INTO tb_operasi_tabel (id_operasi, nama_tabel)
										VALUES ('$ID', '$isianNamaTabel')";
				}
				$queryInsertTabel = mysql_query($sqlInsertTabel);
			}

			// SIMPAN KE TABEL OUTPUT
			$i = 1;
			$sqlLihatOutput = "SELECT * FROM tb_operasi_output WHERE id_operasi = '$ID' ORDER BY id_operasi_output ASC";
			echo $sqlLihatOutput;
			$queryLihatOutput = mysql_query($sqlLihatOutput);
			$jumlah = mysql_num_rows($queryLihatOutput);
			$queryLihatOutput = mysql_query($sqlLihatOutput);
			while ($lihatOutput = mysql_fetch_array($queryLihatOutput)) {
				$id_operasi_output[$i] = $lihatOutput['id_operasi_output'];
				$i++;
			}

			for ($i=1; $i <= $jumlahOutput; $i++) { 
				if ($arrayOutput[$i][3] == "") {
					$isianOutput = $arrayOutput[$i][1]; 
				} else {
					$isianOutput = $arrayOutput[$i][3]."(".$arrayOutput[$i][1].")";
				}
				$isianNamaOutput = $arrayOutput[$i][2];

				if ($jumlah > 0) {
					$sqlInsertOutput = "UPDATE tb_operasi_output SET output='$isianOutput' and nama_output='$isianOutput'
										WHERE id_operasi_output='$id_operasi_output[$i]'";
				} else {
					$sqlInsertOutput = "INSERT INTO tb_operasi_output (id_operasi, output, nama_output)
										VALUES ('$ID', '$isianOutput', '$isianNamaOutput')";
				}
				echo $sqlInsertOutput;
				$queryInsertOutput = mysql_query($sqlInsertOutput);
			}

			// SIMPAN KE TABEL PARAMETER
			$i = 1;
			$sqlLihatParameter = "SELECT * FROM tb_operasi_parameter WHERE id_operasi = '$ID' ORDER BY id_operasi_parameter ASC";
			$queryLihatParameter = mysql_query($sqlLihatParameter);
			$jumlah = mysql_num_rows($queryLihatParameter);
			$queryLihatParameter = mysql_query($sqlLihatParameter);
			while ($lihatParameter = mysql_fetch_array($queryLihatParameter)) {
				$id_operasi_parameter[$i] = $lihatParameter['id_operasi_parameter'];
				$i++;
			}

			for ($i=1; $i <= $jumlahParameter; $i++) { 
				$isianParameter = $arrayParameter[$i][1]; 
				$isianNamaParameter = $arrayParameter[$i][2];
				$isianTypeParameter = $arrayParameter[$i][3];
				if ($jumlah > 0) {
					$sqlInsertParameter = "UPDATE tb_operasi_parameter SET parameter='$isianParameter' and nama_parameter='$isianNamaParameter',
										type='$isianTypeParameter'
										WHERE id_operasi_parameter='$id_operasi_parameter[$i]'";
					$format = $format.$karakter.$isianNamaParameter;
				} else {
					$sqlInsertParameter = "INSERT INTO tb_operasi_parameter (id_operasi, parameter, nama_parameter, is_kunci, type)
									VALUES ('$ID', '$isianParameter', '$isianNamaParameter', '1', '$isianTypeParameter')";
					$format = $format.$karakter.$isianNamaParameter;
				}
				$queryInsertParameter = mysql_query($sqlInsertParameter);
			}
			break;
		
		// =========================================================================================================

		case 'insert':
			// PENGAMBILAN ID PADA TABEL OPERASI
			$sqlID = "SELECT * FROM tb_operasi WHERE keyword = '$keyword' and nama_operasi = '$formatName'";
			$queryID = mysql_query($sqlID);
			$ID = mysql_fetch_array($queryID);
			$ID = $ID['id_operasi'];

			// SIMPAN KE TABEL TABEL
			$i = 1;
			$sqlLihatTabel = "SELECT * FROM tb_operasi_tabel WHERE id_operasi = '$ID' ORDER BY id_operasi_tabel ASC";
			$queryLihatTabel = mysql_query($sqlLihatTabel);
			$jumlah = mysql_num_rows($queryLihatTabel);
			$queryLihatTabel = mysql_query($sqlLihatTabel);
			while ($lihatTabel = mysql_fetch_array($queryLihatTabel)) {
				$id_operasi_tabel[$i] = $lihatTabel['id_operasi_tabel'];
				$i++;
			}

			for ($i=1; $i <= $jumlahTabel; $i++) { 
				$isianNamaTabel = $arrayTabel[$i];
				$id_operasi_tabel = $id_operasi_tabel[$i];
				if ($jumlah > 0) {
					$sqlInsertTabel = "UPDATE tb_operasi_tabel SET nama_tabel = '$isianNamaTabel'
										WHERE id_operasi_tabel = '$id_operasi_tabel'";
				} else{
					$sqlInsertTabel = "INSERT INTO tb_operasi_tabel (id_operasi, nama_tabel)
										VALUES ('$ID', '$isianNamaTabel')";
				}
				echo $sqlInsertTabel;
				$queryInsertTabel = mysql_query($sqlInsertTabel);
			}

			// SIMPAN KE TABEL OUTPUT
			$i = 1;
			$sqlLihatOutput = "SELECT * FROM tb_operasi_output WHERE id_operasi = '$ID' ORDER BY id_operasi_output ASC";
			$queryLihatOutput = mysql_query($sqlLihatOutput);
			$jumlah = mysql_num_rows($queryLihatOutput);
			$queryLihatOutput = mysql_query($sqlLihatOutput);
			while ($lihatOutput = mysql_fetch_array($queryLihatOutput)) {
				$id_operasi_output[$i] = $lihatOutput['id_operasi_output'];
				$i++;
			}

			for ($i=1; $i <= $jumlahOutput; $i++) { 
				$isianOutput = $arrayOutput[$i][1]; 
				$isianNamaOutput = $arrayOutput[$i][2];
				if ($jumlah > 0) {
					$sqlInsertOutput = "UPDATE tb_operasi_output SET output='$isianOutput' and nama_output='$isianOutput'
										WHERE id_operasi_output='$id_operasi_output[$i]'";
					$format = $format.$karakter.$isianNamaOutput;
				} else {
					$sqlInsertOutput = "INSERT INTO tb_operasi_output (id_operasi, output, nama_output)
										VALUES ('$ID', '$isianOutput', '$isianNamaOutput')";
					$format = $format.$karakter.$isianNamaOutput;
				}
				$queryInsertOutput = mysql_query($sqlInsertOutput);
			}
			break;

		// =========================================================================================================

		case 'update':
			// PENGAMBILAN ID PADA TABEL OPERASI
			$sqlID = "SELECT * FROM tb_operasi WHERE keyword = '$keyword' and nama_operasi = '$formatName'";
			$queryID = mysql_query($sqlID);
			$ID = mysql_fetch_array($queryID);
			$ID = $ID['id_operasi'];

			// SIMPAN KE TABEL TABEL
			$i = 1;
			$sqlLihatTabel = "SELECT * FROM tb_operasi_tabel WHERE id_operasi = '$ID' ORDER BY id_operasi_tabel ASC";
			$queryLihatTabel = mysql_query($sqlLihatTabel);
			$jumlah = mysql_num_rows($queryLihatTabel);
			$queryLihatTabel = mysql_query($sqlLihatTabel);
			while ($lihatTabel = mysql_fetch_array($queryLihatTabel)) {
				$id_operasi_tabel[$i] = $lihatTabel['id_operasi_tabel'];
				$i++;
			}

			for ($i=1; $i <= $jumlahTabel; $i++) { 
				$isianNamaTabel = $arrayTabel[$i];
				$id_operasi_tabel = $id_operasi_tabel[$i];
				if ($jumlah > 0) {
					$sqlInsertTabel = "UPDATE tb_operasi_tabel SET nama_tabel = '$isianNamaTabel'
										WHERE id_operasi_tabel = '$id_operasi_tabel'";
				} else{
					$sqlInsertTabel = "INSERT INTO tb_operasi_tabel (id_operasi, nama_tabel)
										VALUES ('$ID', '$isianNamaTabel')";
				}
				echo $sqlInsertTabel;
				$queryInsertTabel = mysql_query($sqlInsertTabel);
			}

			// SIMPAN KE TABEL OUTPUT
			$i = 1;
			$sqlLihatOutput = "SELECT * FROM tb_operasi_output WHERE id_operasi = '$ID' ORDER BY id_operasi_output ASC";
			$queryLihatOutput = mysql_query($sqlLihatOutput);
			$jumlah = mysql_num_rows($queryLihatOutput);
			$queryLihatOutput = mysql_query($sqlLihatOutput);
			while ($lihatOutput = mysql_fetch_array($queryLihatOutput)) {
				$id_operasi_output[$i] = $lihatOutput['id_operasi_output'];
				$i++;
			}

			for ($i=1; $i <= $jumlahOutput; $i++) { 
				$isianOutput = $arrayOutput[$i][1]; 
				$isianNamaOutput = $arrayOutput[$i][2];
				if ($jumlah > 0) {
					$sqlInsertOutput = "UPDATE tb_operasi_output SET output='$isianOutput' and nama_output='$isianOutput'
										WHERE id_operasi_output='$id_operasi_output[$i]'";
					$format = $format.$karakter.$isianNamaOutput;
				} else {
					$sqlInsertOutput = "INSERT INTO tb_operasi_output (id_operasi, output, nama_output)
										VALUES ('$ID', '$isianOutput', '$isianNamaOutput')";
					$format = $format.$karakter.$isianNamaOutput;
				}
				$queryInsertOutput = mysql_query($sqlInsertOutput);
			}

			// SIMPAN KE TABEL PARAMETER
			$i = 1;
			$sqlLihatParameter = "SELECT * FROM tb_operasi_parameter WHERE id_operasi = '$ID' ORDER BY id_operasi_parameter ASC";
			$queryLihatParameter = mysql_query($sqlLihatParameter);
			$jumlah = mysql_num_rows($queryLihatParameter);
			$queryLihatParameter = mysql_query($sqlLihatParameter);
			while ($lihatParameter = mysql_fetch_array($queryLihatParameter)) {
				$id_operasi_parameter[$i] = $lihatParameter['id_operasi_parameter'];
				$i++;
			}

			for ($i=1; $i <= $jumlahParameter; $i++) { 
				$isianParameter = $arrayParameter[$i][1]; 
				$isianNamaParameter = $arrayParameter[$i][2];
				$isianTypeParameter = $arrayParameter[$i][3];
				if ($jumlah > 0) {
					$sqlInsertParameter = "UPDATE tb_operasi_parameter SET parameter='$isianParameter' and nama_parameter='$isianNamaParameter',
										type='$isianTypeParameter'
										WHERE id_operasi_parameter='$id_operasi_parameter[$i]'";
					$format = $format.$karakter.$isianNamaParameter;
				} else {
					$sqlInsertParameter = "INSERT INTO tb_operasi_parameter (id_operasi, parameter, nama_parameter, is_kunci, type)
									VALUES ('$ID', '$isianParameter', '$isianNamaParameter', '1', '$isianTypeParameter')";
					$format = $format.$karakter.$isianNamaParameter;
				}
				$queryInsertParameter = mysql_query($sqlInsertParameter);
			}
			break;

		// =========================================================================================================

		case 'delete':
			// PENGAMBILAN ID PADA TABEL OPERASI
			$sqlID = "SELECT * FROM tb_operasi WHERE keyword = '$keyword' and nama_operasi = '$formatName'";
			$queryID = mysql_query($sqlID);
			$ID = mysql_fetch_array($queryID);
			$ID = $ID['id_operasi'];

			// SIMPAN KE TABEL TABEL
			$i = 1;
			$sqlLihatTabel = "SELECT * FROM tb_operasi_tabel WHERE id_operasi = '$ID' ORDER BY id_operasi_tabel ASC";
			$queryLihatTabel = mysql_query($sqlLihatTabel);
			$jumlah = mysql_num_rows($queryLihatTabel);
			$queryLihatTabel = mysql_query($sqlLihatTabel);
			while ($lihatTabel = mysql_fetch_array($queryLihatTabel)) {
				$id_operasi_tabel[$i] = $lihatTabel['id_operasi_tabel'];
				$i++;
			}

			for ($i=1; $i <= $jumlahTabel; $i++) { 
				$isianNamaTabel = $arrayTabel[$i];
				$id_operasi_tabel = $id_operasi_tabel[$i];
				if ($jumlah > 0) {
					$sqlInsertTabel = "UPDATE tb_operasi_tabel SET nama_tabel = '$isianNamaTabel'
										WHERE id_operasi_tabel = '$id_operasi_tabel'";
				} else{
					$sqlInsertTabel = "INSERT INTO tb_operasi_tabel (id_operasi, nama_tabel)
										VALUES ('$ID', '$isianNamaTabel')";
				}
				echo $sqlInsertTabel;
				$queryInsertTabel = mysql_query($sqlInsertTabel);
			}

			// SIMPAN KE TABEL PARAMETER
			$i = 1;
			$sqlLihatParameter = "SELECT * FROM tb_operasi_parameter WHERE id_operasi = '$ID' ORDER BY id_operasi_parameter ASC";
			$queryLihatParameter = mysql_query($sqlLihatParameter);
			$jumlah = mysql_num_rows($queryLihatParameter);
			$queryLihatParameter = mysql_query($sqlLihatParameter);
			while ($lihatParameter = mysql_fetch_array($queryLihatParameter)) {
				$id_operasi_parameter[$i] = $lihatParameter['id_operasi_parameter'];
				$i++;
			}

			for ($i=1; $i <= $jumlahParameter; $i++) { 
				$isianParameter = $arrayParameter[$i][1]; 
				$isianNamaParameter = $arrayParameter[$i][2];
				$isianTypeParameter = $arrayParameter[$i][3];
				if ($jumlah > 0) {
					$sqlInsertParameter = "UPDATE tb_operasi_parameter SET parameter='$isianParameter' and nama_parameter='$isianNamaParameter',
										type='$isianTypeParameter'
										WHERE id_operasi_parameter='$id_operasi_parameter[$i]'";
					$format = $format.$karakter.$isianNamaParameter;
				} else {
					$sqlInsertParameter = "INSERT INTO tb_operasi_parameter (id_operasi, parameter, nama_parameter, is_kunci, type)
									VALUES ('$ID', '$isianParameter', '$isianNamaParameter', '1', '$isianTypeParameter')";
					$format = $format.$karakter.$isianNamaParameter;
				}
				$queryInsertParameter = mysql_query($sqlInsertParameter);
			}
			break;

		// =========================================================================================================

		case 'function':
			// PENGAMBILAN ID PADA TABEL OPERASI
			$sqlID = "SELECT * FROM tb_operasi WHERE keyword = '$keyword' and nama_operasi = '$formatName'";
			$queryID = mysql_query($sqlID);
			$ID = mysql_fetch_array($queryID);
			$ID = $ID['id_operasi'];

			// SIMPAN KE TABEL PARAMETER
			$i = 1;
			$sqlLihatParameter = "SELECT * FROM tb_operasi_parameter WHERE id_operasi = '$ID' ORDER BY id_operasi_parameter ASC";
			$queryLihatParameter = mysql_query($sqlLihatParameter);
			$jumlah = mysql_num_rows($queryLihatParameter);
			$queryLihatParameter = mysql_query($sqlLihatParameter);
			while ($lihatParameter = mysql_fetch_array($queryLihatParameter)) {
				$id_operasi_parameter[$i] = $lihatParameter['id_operasi_parameter'];
				$i++;
			}

			for ($i=1; $i <= $jumlahFungsiParameter; $i++) { 
				$isianParameter = $arrayFungsiParameter[$i][1]; 
				$isianNamaParameter = $arrayFungsiParameter[$i][2];
				if ($jumlah > 0) {
					$sqlInsertParameter = "UPDATE tb_operasi_parameter SET parameter='$isianParameter' and nama_parameter='$isianNamaParameter',
										type='$isianTypeParameter'
										WHERE id_operasi_parameter='$id_operasi_parameter[$i]'";
					$format = $format.$karakter.$isianNamaParameter;
				} else {
					$sqlInsertParameter = "INSERT INTO tb_operasi_parameter (id_operasi, parameter, nama_parameter, is_kunci)
									VALUES ('$ID', '$isianParameter', '$isianNamaParameter', '1')";
					$format = $format.$karakter.$isianNamaParameter;
				}
				$queryInsertParameter = mysql_query($sqlInsertParameter);
			}
			break;

		// =========================================================================================================

		case 'procedure':
			// PENGAMBILAN ID PADA TABEL OPERASI
			$sqlID = "SELECT * FROM tb_operasi WHERE keyword = '$keyword' and nama_operasi = '$formatName'";
			$queryID = mysql_query($sqlID);
			$ID = mysql_fetch_array($queryID);
			$ID = $ID['id_operasi'];



			// SIMPAN KE TABEL PARAMETER
			$i = 1;
			$sqlLihatParameter = "SELECT * FROM tb_operasi_parameter WHERE id_operasi = '$ID' ORDER BY id_operasi_parameter ASC";
			$queryLihatParameter = mysql_query($sqlLihatParameter);
			$jumlah = mysql_num_rows($queryLihatParameter);
			$queryLihatParameter = mysql_query($sqlLihatParameter);
			while ($lihatParameter = mysql_fetch_array($queryLihatParameter)) {
				$id_operasi_parameter[$i] = $lihatParameter['id_operasi_parameter'];
				$i++;
			}

			for ($i=1; $i <= $jumlahFungsiParameter; $i++) { 
				$isianParameter = $arrayFungsiParameter[$i][1]; 
				$isianNamaParameter = $arrayFungsiParameter[$i][2];
				if ($jumlah > 0) {
					$sqlInsertParameter = "UPDATE tb_operasi_parameter SET parameter='$isianParameter' and nama_parameter='$isianNamaParameter',
										type='$isianTypeParameter'
										WHERE id_operasi_parameter='$id_operasi_parameter[$i]'";
					$format = $format.$karakter.$isianNamaParameter;
				} else {
					$sqlInsertParameter = "INSERT INTO tb_operasi_parameter (id_operasi, parameter, nama_parameter, is_kunci)
									VALUES ('$ID', '$isianParameter', '$isianNamaParameter', '1')";
					$format = $format.$karakter.$isianNamaParameter;
				}
				$queryInsertParameter = mysql_query($sqlInsertParameter);
			}
			break;
	}

	// SIMPAN KE TABEL FORMAT

	$sqlLihatFormat = "SELECT * FROM tb_format WHERE id_operasi = '$ID'";
	$queryLihatFormat = mysql_query($sqlLihatFormat);
	$jumlah = mysql_num_rows($queryLihatFormat);

	if ($jumlah > 0) {
		$queryLihatFormat = mysql_query($sqlLihatFormat);
		$lihatFormat = mysql_fetch_array($queryLihatFormat);
		$id_format = $lihatFormat['id_format'];
		$sqlSimpanFormat = "UPDATE tb_format SET id_operasi='$ID', format='$format'
							WHERE id_format='$id_format'";
	} else{
		$sqlSimpanFormat = "INSERT INTO tb_format (id_operasi, format)
							VALUES ('$ID', '$format')";
	}
	echo $sqlSimpanFormat;
	$querySimpanFormat = mysql_query($sqlSimpanFormat);
?>